/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.Sales;

import entity.Address;
import entity.Inquiry;
import entity.Job;
import entity.Product;
import entity.Quotation;
import entity.QuotationItem;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import managedbean.UserSessionManagerBean;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;
import session.stateless.JobSessionBeanLocal;
import session.stateless.ProductSessionBeanLocal;
import session.stateless.ProductionScheduleSessionBeanLocal;
import session.stateless.WarehouseSessionBeanLocal;

/**
 *
 * @author Wx
 */
@ManagedBean(name = "quotationManagerBean")
@RequestScoped
public class QuotationManagerBean {

    @ManagedProperty(value = "#{userSessionManagerBean}")
    private UserSessionManagerBean userSessionManagerBean;
    @EJB
    ProductSessionBeanLocal productSessionBeanLocal;
    @EJB
    JobSessionBeanLocal jobSessionBeanLocal;
    @EJB
    WarehouseSessionBeanLocal warehouseSessionBeanLocal;
    @EJB
    ProductionScheduleSessionBeanLocal productionScheduleSessionBeanLocal;
    private String loadQuotationPage;
    private Long quotationId;
    private String remark;
    private Inquiry inquiry;
    private double totalAmount;
    private int leadTime;
    private String status;
    private List<QuotationItem> quotationItems;
    private Quotation selectedQuotation;
    private Long quotationItemId;
    private double unitPrice;
    private double subTotal;
    private int quantity;
    private double discount;
    private Product product = new Product();
    private double shippingRate;
    private Address shippingAddress;
    private Date requestDate;
    private String shippingMethod;
    private QuotationItem quotationItem;
    private List<Address> shippingAddresses = new ArrayList<Address>();
    private List<Product> products;
    private int quotationItemsCount;
    private Date minDate;
//    private Boolean fromUpdateJob;
    private Boolean isExpiredQuotation;
    private Boolean isEmptyQuotation;
    private Boolean isAddJob;
    private DateFormat df = new SimpleDateFormat("dd-MM-yyyy");

    /** Creates a new instance of QuotationManagerBean */
    public QuotationManagerBean() {
    }

    /**
     * @return the loadAddQuotation
     */
    public String getLoadQuotationPage() {
        try {
            setProducts(productSessionBeanLocal.getProducts());

            for (Address o : userSessionManagerBean.getSelectedJob().getCustomer().getAddresses()) {
                if (o.getType().compareTo("Shipping") == 0) {
                    shippingAddresses.add(o);
                }
            }
            Calendar cal = Calendar.getInstance();
            requestDate = cal.getTime();
            cal.add(Calendar.DAY_OF_MONTH, 14);
            minDate = cal.getTime();
            requestDate = minDate;

            remark = userSessionManagerBean.getSelectedQuotation().getRemark();
            setQuotationItems(userSessionManagerBean.getSelectedQuotation().getQuotationItems());
            if (userSessionManagerBean.getSelectedQuotation().getStatus().equals("Expired")) {
                isExpiredQuotation = true;
            } else {
                isExpiredQuotation = false;
            }

            if (userSessionManagerBean.getSelectedQuotation().getQuotationItems().size() > 0) {
                isEmptyQuotation = false;
            } else {
                isEmptyQuotation = true;
            }

            if (userSessionManagerBean.getRequest().compareTo("updateJob.xhtml") == 0) {
                isAddJob = false;
            } else {
                isAddJob = true;
            }

            this.setQuotationItemsCount(quotationItems.size());
        } catch (Exception exception) {
            exception.printStackTrace();
            try {
                if (userSessionManagerBean.getRequest() != null) {
                    if (userSessionManagerBean.getRequest().compareTo("addJob.xhtml") == 0) {
                        FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                                + userSessionManagerBean.getServerName() + ":"
                                + userSessionManagerBean.getServerPort()
                                + "/MerlionERP-war/Web_Sales/addJob.xhtml");
                    }
                } else {
                    FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                            + userSessionManagerBean.getServerName() + ":"
                            + userSessionManagerBean.getServerPort()
                            + "/MerlionERP-war/Web_Sales/updateJob.xhtml");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }

    // <editor-fold defaultstate="collapsed" desc="Getter & Setter methods. Click on the + sign on the left to edit the code.">
    /**
     * @param loadAddQuotation the loadAddQuotation to set
     */
    public void setLoadQuotationPage(String loadQuotationPage) {
        this.loadQuotationPage = loadQuotationPage;
    }

    /**
     * @return the quotationId
     */
    public Long getQuotationId() {
        return quotationId;
    }

    /**
     * @param quotationId the quotationId to set
     */
    public void setQuotationId(Long quotationId) {
        this.quotationId = quotationId;
    }

    /**
     * @return the remark
     */
    public String getRemark() {
        return remark;
    }

    /**
     * @param remark the remark to set
     */
    public void setRemark(String remark) {
        this.remark = remark;
    }

    /**
     * @return the inquiry
     */
    public Inquiry getInquiry() {
        return inquiry;
    }

    /**
     * @param inquiry the inquiry to set
     */
    public void setInquiry(Inquiry inquiry) {
        this.inquiry = inquiry;
    }

    /**
     * @return the totalAmount
     */
    public double getTotalAmount() {
        return totalAmount;
    }

    /**
     * @param totalAmount the totalAmount to set
     */
    public void setTotalAmount(double totalAmount) {
        this.totalAmount = totalAmount;
    }

    /**
     * @return the leadTime
     */
    public int getLeadTime() {
        return leadTime;
    }

    /**
     * @param leadTime the leadTime to set
     */
    public void setLeadTime(int leadTime) {
        this.leadTime = leadTime;
    }

    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * @return the quotationItems
     */
    public List<QuotationItem> getQuotationItems() {
        return quotationItems;
    }

    /**
     * @param quotationItems the quotationItems to set
     */
    public void setQuotationItems(List<QuotationItem> quotationItems) {
        this.quotationItems = quotationItems;
    }

    /**
     * @return the quotationItemId
     */
    public Long getQuotationItemId() {
        return quotationItemId;
    }

    /**
     * @param quotationItemId the quotationItemId to set
     */
    public void setQuotationItemId(Long quotationItemId) {
        this.quotationItemId = quotationItemId;
    }

    /**
     * @return the unitPrice
     */
    public double getUnitPrice() {
        return unitPrice;
    }

    /**
     * @param unitPrice the unitPrice to set
     */
    public void setUnitPrice(double unitPrice) {
        this.unitPrice = unitPrice;
    }

    /**
     * @return the subTotal
     */
    public double getSubTotal() {
        return subTotal;
    }

    /**
     * @param subTotal the subTotal to set
     */
    public void setSubTotal(double subTotal) {
        this.subTotal = subTotal;
    }

    /**
     * @return the quantity
     */
    public int getQuantity() {
        return quantity;
    }

    /**
     * @param quantity the quantity to set
     */
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    /**
     * @return the discount
     */
    public double getDiscount() {
        return discount;
    }

    /**
     * @param discount the discount to set
     */
    public void setDiscount(double discount) {
        this.discount = discount;
    }

    /**
     * @return the product
     */
    public Product getProduct() {
        return product;
    }

    /**
     * @param product the product to set
     */
    public void setProduct(Product product) {
        this.product = product;
    }

    /**
     * @return the products
     */
    public List<Product> getProducts() {
        return products;
    }

    /**
     * @param products the products to set
     */
    public void setProducts(List<Product> products) {
        this.products = products;
    }

    /**
     * @return the inquiryItemsCount
     */
    public int getQuotationItemsCount() {
        return quotationItemsCount;
    }

    /**
     * @param inquiryItemsCount the inquiryItemsCount to set
     */
    public void setQuotationItemsCount(int quotationItemsCount) {
        this.quotationItemsCount = quotationItemsCount;
    }

//    /**
//     * @return the fromUpdateJob
//     */
//    public Boolean getFromUpdateJob() {
//        return fromUpdateJob;
//    }
//
//    /**
//     * @param fromUpdateJob the fromUpdateJob to set
//     */
//    public void setFromUpdateJob(Boolean fromUpdateJob) {
//        this.fromUpdateJob = fromUpdateJob;
//    }
    /**
     * @return the userSessionManagerBean
     */
    public UserSessionManagerBean getUserSessionManagerBean() {
        return userSessionManagerBean;
    }

    /**
     * @param userSessionManagerBean the userSessionManagerBean to set
     */
    public void setUserSessionManagerBean(UserSessionManagerBean userSessionManagerBean) {
        this.userSessionManagerBean = userSessionManagerBean;
    }

    /**
     * @return the selectedQuotation
     */
    public Quotation getSelectedQuotation() {
        return selectedQuotation;
    }

    /**
     * @param selectedQuotation the selectedQuotation to set
     */
    public void setSelectedQuotation(Quotation selectedQuotation) {
        this.selectedQuotation = selectedQuotation;
    }

    /**
     * @return the shippingRate
     */
    public double getShippingRate() {
        return shippingRate;
    }

    /**
     * @param shippingRate the shippingRate to set
     */
    public void setShippingRate(double shippingRate) {
        this.shippingRate = shippingRate;
    }

    /**
     * @return the shippingAddress
     */
    public Address getShippingAddress() {
        return shippingAddress;
    }

    /**
     * @param shippingAddress the shippingAddress to set
     */
    public void setShippingAddress(Address shippingAddress) {
        this.shippingAddress = shippingAddress;
    }

    /**
     * @return the shippingAddresses
     */
    public List<Address> getShippingAddresses() {
        return shippingAddresses;
    }

    /**
     * @param shippingAddresses the shippingAddresses to set
     */
    public void setShippingAddresses(List<Address> shippingAddresses) {
        this.shippingAddresses = shippingAddresses;
    }

    /**
     * @return the quotationItem
     */
    public QuotationItem getQuotationItem() {
        return quotationItem;
    }

    /**
     * @param quotationItem the quotationItem to set
     */
    public void setQuotationItem(QuotationItem quotationItem) {
        this.quotationItem = quotationItem;
    }

    /**
     * @return the requestDate
     */
    public Date getRequestDate() {
        return requestDate;
    }

    /**
     * @param requestDate the requestDate to set
     */
    public void setRequestDate(Date requestDate) {
        this.requestDate = requestDate;
    }

    /**
     * @return the shippingMethod
     */
    public String getShippingMethod() {
        return shippingMethod;
    }

    /**
     * @param shippingMethod the shippingMethod to set
     */
    public void setShippingMethod(String shippingMethod) {
        this.shippingMethod = shippingMethod;
    }

    /**
     * @return the isExpiredQuotation
     */
    public Boolean getIsExpiredQuotation() {
        return isExpiredQuotation;
    }

    /**
     * @param isExpiredQuotation the isExpiredQuotation to set
     */
    public void setIsExpiredQuotation(Boolean isExpiredQuotation) {
        this.isExpiredQuotation = isExpiredQuotation;
    }

    /**
     * @return the isEmptyQuotation
     */
    public Boolean getIsEmptyQuotation() {
        return isEmptyQuotation;
    }

    /**
     * @param isEmptyQuotation the isEmptyQuotation to set
     */
    public void setIsEmptyQuotation(Boolean isEmptyQuotation) {
        this.isEmptyQuotation = isEmptyQuotation;
    }

    /**
     * @return the minDate
     */
    public Date getMinDate() {
        return minDate;
    }

    /**
     * @param minDate the minDate to set
     */
    public void setMinDate(Date minDate) {
        this.minDate = minDate;
    }

    // </editor-fold> 
    public void addQuotationItem(ActionEvent event) { //to add an inquiry item to an inquiry
        try {
            if (product.getCode() != null) { //if product selected
                Product p = productSessionBeanLocal.getProduct(product.getCode());

                if (shippingAddress != null) { //if shippingAddress selected
                    if (!shippingMethod.equals("")) { //if shippingMethod selected

                        if (shippingMethod.compareTo("FOB") == 0) {
                            shippingRate = 0.3;
                        } else {
                            shippingRate = 1.0;
                        }

                        unitPrice = p.getPrice();
                        discount = round(discount, 2);
                        double actual = 100 - discount;
                        actual = actual / 100;

                        int cases = quantity / 240; //finding out number of cases required for quantity
                        if (quantity - (cases * 240) > 0) { //240bars per case
                            cases++;
                        }

                        subTotal = ((unitPrice * quantity) * actual) + (cases * shippingRate);
                        subTotal = round(subTotal, 2);

                        //ATP
                        Date d = Calendar.getInstance().getTime();
                        String current = df.format(d);
                        String str[] = current.split("-");
                        Integer month = Integer.parseInt(str[1]);
                        Integer year = Integer.parseInt(str[2]);

                        String request = df.format(requestDate);
                        String str2[] = request.split("-");
                        Date backDate = requestDate;

                        requestDate = null;

                        //ATP Inventory
                        if (month.compareTo(Integer.parseInt(str2[1])) == 0 && year.compareTo(Integer.parseInt(str2[2])) == 0) {
                            requestDate = warehouseSessionBeanLocal.checkDateFromInventory(p.getCode(), quantity);
                            Calendar cal = Calendar.getInstance();
                            cal.setTime(backDate);
                            Calendar cal2 = Calendar.getInstance();

                            //check inventory
                            if (requestDate != null) {
                                cal2.setTime(requestDate);
                                if (cal.after(cal2)) {
                                    requestDate = cal.getTime();
                                }
                            }
                        }
                        //Check schedule
                        if (requestDate == null) {
                            requestDate = productionScheduleSessionBeanLocal.checkDateFromPlan(p.getCode(), quantity, Integer.parseInt(str2[1]), Integer.parseInt(str2[2]));
                        }

                        if (requestDate != null) {
                            QuotationItem qi = new QuotationItem(unitPrice, subTotal, quantity, discount, p,
                                    shippingRate, shippingAddress, requestDate, shippingMethod);

                            if (userSessionManagerBean.getRequest().compareTo("updateJob.xhtml") == 0) { //if coming from updateJob

                                //add quotation item into quotation
                                jobSessionBeanLocal.updateQuotation(userSessionManagerBean.getSelectedQuotation().getId(), qi);

                                //get updated quotation with newly added quotation item from above
                                Quotation q = jobSessionBeanLocal.getQuotation(userSessionManagerBean.getSelectedQuotation().getId());
                                List<QuotationItem> items = q.getQuotationItems();

                                double quotationTotalAmount = 0;
                                double quotationSubTotal = 0;
                                double quotationTotalShipping = 0;

                                for (QuotationItem item : items) {
                                    quotationTotalAmount += item.getSubTotal(); //quotation's total amount = total of all items' subtotal

                                    int tCases = item.getQuantity() / 240; //finding out number of cases required for quantity
                                    if (item.getQuantity() - (tCases * 240) > 0) { //240 bars per case
                                        tCases++;
                                    }

                                    double tTotalShipping = tCases * item.getShippingRate();
                                    quotationTotalShipping += tTotalShipping; //quotation's total shipping

                                    quotationSubTotal += item.getSubTotal() - tTotalShipping;
                                }

                                quotationSubTotal = round(quotationSubTotal, 2);
                                quotationTotalShipping = round(quotationTotalShipping, 2);
                                quotationTotalAmount = round(quotationTotalAmount, 2);

                                q.setTotalAmount(quotationTotalAmount);
                                q.setTotalShipping(quotationTotalShipping);
                                q.setSubTotal(quotationSubTotal);

                                jobSessionBeanLocal.updateQuotation(
                                        userSessionManagerBean.getSelectedQuotation().getId(), quotationTotalAmount, quotationSubTotal, quotationTotalShipping, remark);

                                userSessionManagerBean.setSelectedQuotation(jobSessionBeanLocal. //retrieve and set updated quotation 
                                        getQuotation(userSessionManagerBean.getSelectedQuotation().getId())); //from DB
                            } else { //if coming from addJob
                                Quotation q = userSessionManagerBean.getSelectedQuotation();

                                userSessionManagerBean.getSelectedQuotation().getQuotationItems().add(qi);

                                double quotationTotalAmount = 0;
                                double quotationSubTotal = 0;
                                double quotationTotalShipping = 0;

                                for (QuotationItem item : userSessionManagerBean.getSelectedQuotation().getQuotationItems()) {
                                    quotationTotalAmount += item.getSubTotal();  //quotation's total amount = total of all items' subtotal

                                    int tCases = item.getQuantity() / 240; //finding out number of cases required for quantity
                                    if (item.getQuantity() - (tCases * 240) > 0) { //240 bars per case
                                        tCases++;
                                    }

                                    double tTotalShipping = tCases * item.getShippingRate();
                                    quotationTotalShipping += tTotalShipping; //quotation's total shipping

                                    quotationSubTotal += item.getSubTotal() - tTotalShipping;
                                }
                                
                                quotationSubTotal = round(quotationSubTotal, 2);
                                quotationTotalShipping = round(quotationTotalShipping, 2);
                                quotationTotalAmount = round(quotationTotalAmount, 2);

                                userSessionManagerBean.getSelectedQuotation().setTotalAmount(quotationTotalAmount);
                                userSessionManagerBean.getSelectedQuotation().setTotalShipping(quotationTotalShipping);
                                userSessionManagerBean.getSelectedQuotation().setSubTotal(quotationSubTotal);
                                userSessionManagerBean.getSelectedQuotation().setRemark(remark);
                            }

                            FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                                    + userSessionManagerBean.getServerName() + ":"
                                    + userSessionManagerBean.getServerPort()
                                    + "/MerlionERP-war/Web_Sales/quotation.xhtml"); //redirect to quotation.xhtml
                        } else {
                            FacesContext.getCurrentInstance().addMessage(null,
                                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Production schedule not ready to fulfill this order.", ""));
                        }
                    } else {
                        FacesContext.getCurrentInstance().addMessage(null,
                                new FacesMessage(FacesMessage.SEVERITY_ERROR, "A shipping method must be selected.", ""));
                    }
                } else {
                    FacesContext.getCurrentInstance().addMessage(null,
                            new FacesMessage(FacesMessage.SEVERITY_ERROR, "A shipping address must be selected.", ""));
                }
            } else {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "A product must be selected.", ""));
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public double round(double valueToRound, int numberOfDecimalPlaces) {
        double multiplicationFactor = Math.pow(10, numberOfDecimalPlaces);
        double interestedInZeroDPs = valueToRound * multiplicationFactor;

        return Math.round(interestedInZeroDPs) / multiplicationFactor;
    }

    public void back(ActionEvent e) {
        try {
            if (userSessionManagerBean.getRequest().compareTo("updateJob.xhtml") == 0) { //go back to updateJob.xhtml
                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + getUserSessionManagerBean().getServerName() + ":"
                        + getUserSessionManagerBean().getServerPort()
                        + "/MerlionERP-war/Web_Sales/updateJob.xhtml"); //redirect to updateJob.xhtml
            } else {
                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + getUserSessionManagerBean().getServerName() + ":"
                        + getUserSessionManagerBean().getServerPort()
                        + "/MerlionERP-war/Web_Sales/addJob.xhtml"); //redirect to addJob.xhtml
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void saveQuotation(ActionEvent e) {
        try {
            if (userSessionManagerBean.getRequest().compareTo("updateJob.xhtml") == 0) { //go back to updateJob.xhtml
                jobSessionBeanLocal.updateQuotation(userSessionManagerBean.getSelectedQuotation().getId(), remark);

                //get updated job with newly added quotation from abive
                Job j = jobSessionBeanLocal.getJob(userSessionManagerBean.getSelectedJob().getJobId());

                //set job to session variable
                userSessionManagerBean.setSelectedItem(j);

                if (userSessionManagerBean.getSelectedQuotation().getQuotationItems().size() > 0) { //quotation has at least one item
                    FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                            + getUserSessionManagerBean().getServerName() + ":"
                            + getUserSessionManagerBean().getServerPort()
                            + "/MerlionERP-war/Web_Sales/updateJob.xhtml"); //redirect to updateJob.xhtml
                } else { //quotation has zero items
                    FacesContext.getCurrentInstance().addMessage(null,
                            new FacesMessage(FacesMessage.SEVERITY_ERROR, "Quotation cannot have zero items.", ""));
                }
            } else { //go back to addJob.xhtml
                Quotation q = userSessionManagerBean.getSelectedQuotation();
                q.setRemark(remark);

                boolean found = false;
                for (Quotation quotation : ((Job) userSessionManagerBean.getSelectedItem()).getQuotations()) {
                    if (quotation.equals(q)) {
                        found = true;
                    }
                    quotation.setStatus("Expired"); //expire previous quotations
                }

                if (!found) {
                    ((Job) userSessionManagerBean.getSelectedItem()).getQuotations().add(q); //add the new quotation
                }

                if (userSessionManagerBean.getSelectedQuotation().getQuotationItems().size() > 0) { //quotation has at least one item
                    FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                            + getUserSessionManagerBean().getServerName() + ":"
                            + getUserSessionManagerBean().getServerPort()
                            + "/MerlionERP-war/Web_Sales/addJob.xhtml"); //redirect to addJob.xhtml
                } else { //quotation has zero items
                    FacesContext.getCurrentInstance().addMessage(null,
                            new FacesMessage(FacesMessage.SEVERITY_ERROR, "Quotation cannot have zero items.", ""));
                }
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void onRowSelect(SelectEvent event) { //on row select of datatable
        try {
            userSessionManagerBean.setSelectedQuotation(selectedQuotation);

            FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                    + userSessionManagerBean.getServerName() + ":"
                    + userSessionManagerBean.getServerPort()
                    + "/MerlionERP-war/Web_Sales/quotation.xhtml"); //redirect to quotation.xhtml on quotation select
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void onRowUnselect(UnselectEvent event) {
        try {
            if (event.getComponent().getId().equals("salesOrders")) {
                shippingAddress = null;
            } else if (event.getComponent().getId().equals("quotationItems")) {
                quotationItem = null;
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void delete(ActionEvent event) { //to delete a quotation item
        try {
            if (quotationItem != null) {
                if (userSessionManagerBean.getRequest().compareTo("updateJob.xhtml") == 0) {
                    jobSessionBeanLocal.deleteQuotationItem(userSessionManagerBean.getSelectedQuotation().getId(), quotationItem.getId());

                    userSessionManagerBean.setSelectedQuotation(
                            jobSessionBeanLocal.getQuotation(userSessionManagerBean.getSelectedQuotation().getId()));
                } else {
                    quotationItems.remove(quotationItem);
                }

                if (userSessionManagerBean.getSelectedQuotation().getQuotationItems().size() <= 0) {
                    isEmptyQuotation = true;
                } else {
                    isEmptyQuotation = false;
                }

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + getUserSessionManagerBean().getServerName() + ":"
                        + getUserSessionManagerBean().getServerPort()
                        + "/MerlionERP-war/Web_Sales/quotation.xhtml"); //redirect to quotation.xhtml
            } else {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select a quotation item to delete.", ""));
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    /**
     * @return the isAddJob
     */
    public Boolean getIsAddJob() {
        return isAddJob;
    }

    /**
     * @param isAddJob the isAddJob to set
     */
    public void setIsAddJob(Boolean isAddJob) {
        this.isAddJob = isAddJob;
    }
}
